﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Zero.UControl.Component
{
    public class RadialToolbar : ItemsControl
    {
        private Border outerBd;

        private bool _isExpanded;
        static RadialToolbar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(RadialToolbar), new FrameworkPropertyMetadata(typeof(RadialToolbar)));
        }

        public string Icon
        {
            get { return (string)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register(nameof(Icon), typeof(string), typeof(RadialToolbar));

        protected override bool IsItemItsOwnContainerOverride(object item)
            => item is RadialToolbarItem;

        protected override DependencyObject GetContainerForItemOverride()
            => new RadialToolbarItem();

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            outerBd = GetTemplateChild("outerBd") as Border;

            outerBd.MouseLeftButtonDown += Border_MouseLeftButtonDown;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            double offsetAngle = -90;
            double radius = ActualWidth / 2 + 50;

            if (!_isExpanded)
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    var container = (RadialToolbarItem)ItemContainerGenerator.ContainerFromIndex(i);
                    if (container == null) continue;

                    double angleDelta = 25 + (40 * i);
                    double targetX = radius * Math.Cos((offsetAngle + angleDelta) * Math.PI / 180);
                    double targetY = radius * Math.Sin((offsetAngle + angleDelta) * Math.PI / 180);

                    var animationX = new DoubleAnimation(targetX, TimeSpan.FromMilliseconds(300))
                    { EasingFunction = new BackEase { EasingMode = EasingMode.EaseOut } };

                    var animationY = new DoubleAnimation(targetY, TimeSpan.FromMilliseconds(300))
                    { EasingFunction = new BackEase { EasingMode = EasingMode.EaseOut } };

                    if (container.RenderTransform is not TranslateTransform)
                        container.RenderTransform = new TranslateTransform();

                    container.RenderTransform.BeginAnimation(TranslateTransform.XProperty, animationX);
                    container.RenderTransform.BeginAnimation(TranslateTransform.YProperty, animationY);
                }
            }
            else
            {
                var animationX = new DoubleAnimation(0, TimeSpan.FromMilliseconds(300));
                var animationY = new DoubleAnimation(0, TimeSpan.FromMilliseconds(300));

                for (int i = 0; i < Items.Count; i++)
                {
                    var container = (RadialToolbarItem)ItemContainerGenerator.ContainerFromIndex(i);
                    if (container == null) continue;

                    if (container.RenderTransform is not TranslateTransform)
                        container.RenderTransform = new TranslateTransform();

                    container.RenderTransform.BeginAnimation(TranslateTransform.XProperty, animationX);
                    container.RenderTransform.BeginAnimation(TranslateTransform.YProperty, animationY);
                }
            }

            _isExpanded = !_isExpanded;
        }

    }
}
